package com.wanchuang.shengfubao.module.machines;
/**
 * 文件名 : AllocaPartnerActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/26 15:53
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.adapter.AllocMachineAdapter;
import com.wanchuang.shengfubao.adapter.AllocMachineBackAdapter;
import com.wanchuang.shengfubao.adapter.SnAllowBean;
import com.wanchuang.shengfubao.base.BaseActivity;
import com.wanchuang.shengfubao.net.Apis;
import com.wanchuang.shengfubao.net.NetworkManager;
import com.wanchuang.shengfubao.net.interfaces.OnRequestCallBack;
import com.wanchuang.shengfubao.proto.Mystock;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by AndroidIntexh1 on 2018/12/27.
 */

public class AllocMachineActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private AllocMachineAdapter mAdapter;
    private AllocMachineBackAdapter mBackAdapter;
    private List<SnAllowBean> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alloca_partner);
        ButterKnife.bind(this);
        backIv.setOnClickListener(view -> finish());
        tvTitle.setText(getIntent().getStringExtra("title"));
        initRecyclerView();
        initData();
    }

    private void initData() {
        Mystock.myMachineType.Builder builder = Mystock.myMachineType.newBuilder();
        builder.setMemberId(getIntent().getStringExtra("member_id"));
        builder.setOther(getIntent().getType());
        NetworkManager.INSTANCE.post(Apis.myMachineType, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                List<Mystock.myMachineType.goodsList> temp_datas = Mystock.myMachineType.parseFrom(response).getGoodsInfoListList();
                for (int i = 0; i < temp_datas.size(); i++) {

                    Log.e("temp_datas", "onOk: " + "1");

                    SnAllowBean titleBean = new SnAllowBean();
                    titleBean.setItemType(0);
                    titleBean.setTitle(temp_datas.get(i).getTabName());
                    datas.add(titleBean);
                    for (int j = 0; j < temp_datas.get(i).getGoodsListList().size(); j++) {
                        SnAllowBean allowBean = new SnAllowBean();
                        allowBean.setItemType(1);
                        allowBean.setItem(temp_datas.get(i).getGoodsListList().get(j));
                        datas.add(allowBean);
                    }
                }
                if (getIntent().getType().equals("0")) {
                    mAdapter.notifyDataSetChanged();
                } else {
                    mBackAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        if (getIntent().getType().equals("0")) {
            recyclerView.setAdapter(mAdapter = new AllocMachineAdapter(mContext, datas));
            mAdapter.setEmptyView(R.layout.recycle_empty);
            // 每个item的点击事件
            mAdapter.setOnItemClickListener((adapter, view, position) -> {
                SnAllowBean bean = datas.get(position);

                if (bean.getItemType() == 0) return;

                if (bean.getItem().getIsPolicy().equals("1") && bean.getItem().getIsStock().equals("1") && bean.getItem().getIsPolicyOther().equals("1")) {
                    Intent intent = new Intent();
                    intent.putExtra("goods_id", bean.getItem().getGoodsCommonid());
                    intent.putExtra("goods_name", bean.getItem().getGoodsName());
                    setResult(300, intent);
                    finish();
                }
            });
        } else {
            recyclerView.setAdapter(mBackAdapter = new AllocMachineBackAdapter(mContext, datas));
            mBackAdapter.setEmptyView(R.layout.recycle_empty);
            // 每个item的点击事件
            mBackAdapter.setOnItemClickListener((adapter, view, position) -> {
                SnAllowBean bean = datas.get(position);
                if (bean.getItemType() == 0) return;
                if (bean.getItem().getIsPolicy().equals("1") && bean.getItem().getIsStock().equals("1")) {
                    Intent intent = new Intent();
                    intent.putExtra("goods_id", bean.getItem().getGoodsCommonid());
                    intent.putExtra("goods_name", bean.getItem().getGoodsName());
                    setResult(300, intent);
                    finish();
                }

            });
        }


    }
}
